package com.shengdong.water.code.utils

import android.content.Context
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

object UploadUtil {
    /**
     * 将图片文件转换为MultipartBody.Part
     */
    fun createImagePart(file: File): MultipartBody.Part {
        val requestBody = file.asRequestBody("image/*".toMediaTypeOrNull())
        return MultipartBody.Part.createFormData("file", file.name, requestBody)
    }

    /**
     * 将Uri转换为File
     */
    fun getFileFromPath(context: Context, path: String): File? {
        return try {
            File(path)
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }
}